package com.hlg.xboot.model.sys;

import com.hlg.xboot.core.annotation.IgnoreQuery;
import com.hlg.xboot.model.BaseModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>Description:系统用户</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author gaol
 * @version 1.0
 * @date 2016/10/27
 */
@Entity
public class SysUser extends BaseModel {

    /**
     * 用户名
     */
    @NotEmpty(message = "{SysUserModel.username.NotEmpty}")
    @Length(max = 20, message = "{SysUserModel.username.Length}")
    @Column(nullable = false, length = 20, updatable = false)
    private String username;

    /**
     * 密码
     */
    @NotEmpty(message = "{SysUserModel.password.NotEmpty}")
    @Length(max = 32, message = "{SysUserModel.password.Length}")
    @Column(length = 32, nullable = false)
    private String password;

    /**
     * 性别
     * 0 男
     * 1 女
     * 2 未知
     */
    @NotNull(message = "{SysUserModel.sex.NotNull}")
    @Column(length = 1, nullable = false)
    private Integer sex;

    /**
     * 手机
     */
    @Length(max = 11, message = "{SysUserModel.phone.Length}")
    @Column(length = 11)
    private String phone;

    /**
     * 状态
     * 1 启用
     * 2 禁用
     */
    @NotNull(message = "{Common.states.NotNull}")
    @Column(length = 1, nullable = false)
    private Integer states;

    /**
     * 头像URL
     */
    private String avatar;

    /**
     * 最后一次登录IP
     */
    @Column(name = "last_login_ip")
    private String lastLoginIP;

    /**
     * 最后一次登录时间
     */
    @Column(name = "last_login_time")
    private Date lastLoginTime;

    /**
     * 备注
     */
    @Length(max = 50, message = "{SysUserModel.remark.Length}")
    @Column(length = 50)
    private String remark;

    /**
     * 是否锁定登录
     */
    @JsonIgnore
    @Transient
    @IgnoreQuery
    private Boolean isLock;

    /**
     * 用户邮箱
     */
    @Column(nullable = false, length = 50)
    private String email;

    /**
     * 真实姓名
     */
    private String realName;


    /**
     * 用户角色
     */
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinTable(name = "user_role",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
            , inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false))
    private List<Role> roles = new ArrayList();

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getStates() {
        return states;
    }

    public void setStates(Integer states) {
        this.states = states;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getUsername() {
        return username;
    }

    public Boolean isLock() {
        return isLock;
    }

    public void setLock(Boolean lock) {
        isLock = lock;
    }

    public String getLastLoginIP() {
        return lastLoginIP;
    }

    public void setLastLoginIP(String lastLoginIP) {
        this.lastLoginIP = lastLoginIP;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    /**
     * 获取权限名称集合
     *
     * @return
     */
    public Set<String> getRoleCodes() {
        Set<String> rolesSet = new HashSet<>();
        for (Role role : this.getRoles()) {
            rolesSet.add(String.valueOf(role.getCode()));
        }
        return rolesSet;
    }
}
